/**
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.base
{	
	import com.actionpals.events.NetConnectionEvent;
	
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;

	public class BaseNetConnection extends NetConnection implements IBaseInterface
	{
		
	//--------------------------------------------------------------------------------
	//
	//	Public Static Constants
	//
	//--------------------------------------------------------------------------------
		
		public static const CALL_BAD_VERSION:String = "NetConnection.Call.BadVersion";
		public static const CALL_FAILED:String = "NetConnection.Call.Failed";
		public static const CALL_PROHIBITED:String = "NetConnection.Call.Prohibited";
		public static const CONNECT_CLOSED:String = "NetConnection.Connect.Closed";
		public static const CONNECT_FAILED:String = "NetConnection.Connect.Failed";
		public static const CONNECT_SUCCESS:String = "NetConnection.Connect.Success";
		public static const CONNECT_REJECTED:String = "NetConnection.Connect.Rejected";
		public static const CONNECT_APP_SHUTDOWN:String = "NetConnection.Connect.AppShutdown";
		public static const CONNECT_INVALID_APP:String = "NetConnection.Connect.InvalidApp";
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Member Variables
	//
	//--------------------------------------------------------------------------------
		
		protected var _eventsBubble:Boolean = false;
		protected var _eventsCancelable:Boolean = false;
		
	//--------------------------------------------------------------------------------
	//
	//	Constructor
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * Constructor
		 */
		public function BaseNetConnection():void
		{	
			super();
			addEventListener( NetStatusEvent.NET_STATUS, onNetStatus );		
		}
		
	//--------------------------------------------------------------------------------
	//	Public Methods
	//--------------------------------------------------------------------------------
		
		public function set eventsBubble( val:Boolean ):void
		{
			_eventsBubble = val;
		}
		
		public function set eventsCancelable( val:Boolean ):void
		{
			_eventsCancelable = val;
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Getters and Setters
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * Returns a fully qualifed class name of BaseClass or any subclass.
		 * @return String		Returns a fully qualifed class name of BaseClass or
		 * 						any subclass.
		 */
		public function get qualifiedClassName():String
		{
			return getQualifiedClassName( this );
		}
		
		/**
		 * Returns a fully qualifed class name of BaseClass or any subclass.
		 * @return A fully qualified class name.
		 */
		public function get qualifiedSuperclassName():String
		{
			return getQualifiedSuperclassName( this );
		}
		
		/**
		 * Returns a fully qualifed class name of BaseClass or any subclass.
		 * @return String A fully qualified class name.
		 */
		public function get typeDescription():XML
		{
			return describeType( this );
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Event Handlers
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * 
		 */
		protected function onNetStatus( event:NetStatusEvent ):void
		{	
			var code:String;
			var ncEvent:NetConnectionEvent;
			
			switch ( event.info.code )
			{	
                case CALL_BAD_VERSION:
                
                    code = NetConnectionEvent.CALL_BAD_VERSION;
                    break;
				
                case CALL_FAILED:
                
                    code = NetConnectionEvent.CALL_FAILED;
                    break;
				
                case CONNECT_CLOSED:
                
                    code = NetConnectionEvent.CONNECT_CLOSED;
                    break;
				
                case CONNECT_FAILED:
                
                    code = NetConnectionEvent.CONNECT_FAILED;
                    break;
				
                case CONNECT_SUCCESS:
                
                    code = NetConnectionEvent.CONNECT_SUCCESS;
                    break;
				
                case CONNECT_REJECTED:
                
                    code = NetConnectionEvent.CONNECT_REJECTED;
                    break;
				
                case CONNECT_APP_SHUTDOWN:
                
                    code = NetConnectionEvent.CONNECT_APP_SHUTDOWN;
                    break;
				
                case CONNECT_INVALID_APP:
                
                    code = NetConnectionEvent.CONNECT_INVALID_APP;
                    break; 
			}
			
			ncEvent = new NetConnectionEvent( code, _eventsBubble, _eventsCancelable );
			dispatchEvent( ncEvent );
		}
	}
}